package com.shen.bq;

import java.util.concurrent.SynchronousQueue;

public class SynchronousQueueTest {
    public static void main(String[] args) {
        /**
         * 同步队列
         */
        SynchronousQueue synchronousQueue = new SynchronousQueue();

        new Thread(()->{

            try {
                System.out.println(Thread.currentThread().getName()+" put a");
                synchronousQueue.put("a");
                System.out.println(Thread.currentThread().getName()+" put b");
                synchronousQueue.put("b");
                System.out.println(Thread.currentThread().getName()+" put c");
                synchronousQueue.put("c");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()->{

            try {
                System.out.println(Thread.currentThread().getName()+" get a");
                System.out.println(synchronousQueue.take());
                System.out.println(Thread.currentThread().getName()+" get b");
                System.out.println(synchronousQueue.take());
                System.out.println(Thread.currentThread().getName()+" get c");
                System.out.println(synchronousQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();

        /**
         * Thread-0 put a
         * Thread-1 get a
         * a
         * Thread-1 get b  //这里执行了get b 但同步队列没有b元素所以take方法不能取
         * Thread-0 put b   //put b
         * Thread-0 put c   //get b
         * b
         * Thread-1 get c
         * c
         */
    }
}
